﻿using System;
using System.Linq.Expressions;

namespace Risesun.Extend
{
    public static class ExpressExtend
    {
        public static Expression Convert(this Expression express, Type type)
        {
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.String:
                    return express;
                case TypeCode.Int32:
                    return Expression.Call(typeof (ConvertHelper), "ToInt", null, express);
                case TypeCode.Single:
                    return Expression.Call(typeof (ConvertHelper), "ToFloat", null, express);
                case TypeCode.Boolean:
                    return Expression.Call(typeof (ConvertHelper), "ToBool", null, express);
                case TypeCode.Decimal:
                    return Expression.Call(typeof (ConvertHelper), "ToDecimal", null, express);
                case TypeCode.Int16:
                    return Expression.Call(typeof (ConvertHelper), "ToShort", null, express);
                case TypeCode.Int64:
                    return Expression.Call(typeof (ConvertHelper), "ToLong", null, express);
                case TypeCode.Double:
                    return Expression.Call(typeof (ConvertHelper), "ToDouble", null, express);
                case TypeCode.DateTime:
                    return Expression.Call(typeof (ConvertHelper), "ToDateTime", null, express);
                default :
                    if (type == typeof (Guid))
                    {
                        return Expression.Call(typeof(ConvertHelper), "ToGuid", null, express);
                    }
                    if (type.IsEnum)
                    {
                        return Expression.Convert(Expression.Call(typeof (ConvertHelper), "ToInt", null, express), type);
                    }
                    break;
            }
            return null;
        }

        public static Expression ConvertList(this Expression express, Type type)
        {
            switch (Type.GetTypeCode(type))
            {
                case TypeCode.String:
                    return express;
                case TypeCode.Int32:
                    return Expression.Call(typeof(ConvertHelper), "ToIntList", null, express);
                case TypeCode.Single:
                    return Expression.Call(typeof(ConvertHelper), "ToFloatList", null, express);
                case TypeCode.Boolean:
                    return Expression.Call(typeof(ConvertHelper), "ToBoolList", null, express);
                case TypeCode.Decimal:
                    return Expression.Call(typeof(ConvertHelper), "ToDecimalList", null, express);
                case TypeCode.Int16:
                    return Expression.Call(typeof(ConvertHelper), "ToShortList", null, express);
                case TypeCode.Int64:
                    return Expression.Call(typeof(ConvertHelper), "ToLongList", null, express);
                case TypeCode.Double:
                    return Expression.Call(typeof(ConvertHelper), "ToDoubleList", null, express);
                case TypeCode.DateTime:
                    return Expression.Call(typeof(ConvertHelper), "ToDateTimeList", null, express);
                default:
                    if (type == typeof(Guid))
                    {
                        return Expression.Call(typeof(ConvertHelper), "ToGuidList", null, express);
                    }
                    if (type.IsEnum)
                    {
                        return Expression.Convert(Expression.Call(typeof(ConvertHelper), "ToIntList", null, express), type);
                    }
                    break;
            }
            return null;
        }
    }
}